package com.reverie.util;

import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class RandomUtil {

    public static String[] chars = new String[]{"2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",
            "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};
    public static String[] charsZM = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};

    public static String getCouponStartZm() {
        StringBuilder startFix = new StringBuilder();
        for (int i = 0; i < 2; i++) {
            int num = (int) (Math.random() * 10);
            startFix.append(charsZM[num]);
        }
        return startFix.toString();
    }

    public static String getCouponEnd() {
        StringBuilder endFix = new StringBuilder();
        for (int i = 0; i < 8; i++) {
            int num = (int) (Math.random() * (chars.length - 1));
            endFix.append(chars[num]);
        }
        return endFix.toString();
    }

    public static String generate(String startChar) {
        int coupon = (int) ((Math.random() * 9 + 1) * 10000000);
        String start = StringUtils.hasLength(startChar) ? startChar : getCouponStartZm().toUpperCase();
        String end = StringUtils.hasLength(startChar) ? getCouponEnd() : String.valueOf(coupon);
        return start + end;
    }

    public static String getOrderCode(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMdd");
        Date date = new Date();
        String str = simpleDateFormat.format(date);
        Random random = new Random();
        int uranium = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数
        return str + uranium;// 当前时间
    }
}
